package com.lancoo.znbkxx.teacher.diagnosis

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.animation.DecelerateInterpolator
import android.view.animation.LinearInterpolator
import com.lancoo.znbkxx.teacher.widget.WaveView

/**
 * @author : Admin
 * @date   : 2020/12/15
 * @desc   :
 */


class WaveHelper(private val mWaveView: WaveView) {
    private val mAnimatorSet = AnimatorSet()
    fun start() {
        mWaveView.isShowWave = true
        initAnimation()
        mAnimatorSet.start()
    }

    private fun initAnimation() {
        val animators: MutableList<Animator> = ArrayList()

        // horizontal animation.
        // wave waves infinitely.
        val waveShiftAnim: ObjectAnimator = ObjectAnimator.ofFloat(mWaveView, "waveShiftRatio", 0f,
            1f)
        waveShiftAnim.repeatCount = ValueAnimator.INFINITE
        waveShiftAnim.duration = 1000
        waveShiftAnim.interpolator = LinearInterpolator()
        animators.add(waveShiftAnim)

        // vertical animation.
        // water level increases from 0 to center of WaveView
        val waterLevelAnim: ObjectAnimator = ObjectAnimator.ofFloat(mWaveView, "waterLevelRatio",
            0f, mWaveView.waterLevelRatio)
        waterLevelAnim.duration = 3000
        waterLevelAnim.interpolator = DecelerateInterpolator()
        animators.add(waterLevelAnim)

        // amplitude animation.
        // wave grows big then grows small, repeatedly
        val amplitudeAnim: ObjectAnimator = ObjectAnimator.ofFloat(mWaveView, "amplitudeRatio",
            mWaveView.amplitudeRatio, mWaveView.amplitudeRatio)
        amplitudeAnim.repeatCount = ValueAnimator.INFINITE
        amplitudeAnim.repeatMode = ValueAnimator.REVERSE
        amplitudeAnim.duration = 5000
        amplitudeAnim.interpolator = LinearInterpolator()
        animators.add(amplitudeAnim)
        mAnimatorSet.playTogether(animators)
    }

    fun cancel() {
        mAnimatorSet?.end()
    }
}
